package com.example.EatOnTrojans;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;  
import java.util.List; 

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element; 
import org.xml.sax.SAXException;

public class DomXMLReader {
	
	public ArrayList<RestRecord> readXML(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {  
		ArrayList<RestRecord> list = new ArrayList<RestRecord>();  
		//find the xml file
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
	    DocumentBuilder builder = factory.newDocumentBuilder(); 
	    Document document=builder.parse(inputStream);
	    //get the root element <root>
	    Element root = document.getDocumentElement();
        //get the elements <restaurant> and put them into a restaurantList
        NodeList restaurantList = root.getElementsByTagName("Restaurant");
        //find all the restaurant nodes
        for (int i=0;i<restaurantList.getLength(); i++) {
        	RestRecord restaurant=new RestRecord();
        	//find the first restaurant node
        	Element restaurantNode = (Element) restaurantList.item(i);
        	NodeList childNodes = restaurantNode.getChildNodes(); 
        	for(int j=0;j<childNodes.getLength();j++){  
        		if(childNodes.item(j).getNodeType()==Node.ELEMENT_NODE){ 
        			if("name".equals(childNodes.item(j).getNodeName())){  
        				restaurant.setName(childNodes.item(j).getFirstChild().getNodeValue());
        			}
        			else if("location".equals(childNodes.item(j).getNodeName())){  
        				restaurant.setLocation(childNodes.item(j).getFirstChild().getNodeValue());
        			}
        			else if("type".equals(childNodes.item(j).getNodeName())){  
        				restaurant.setType(childNodes.item(j).getFirstChild().getNodeValue());
        			}
        			else if("cuisine".equals(childNodes.item(j).getNodeName())){  
        				restaurant.setCuisine(childNodes.item(j).getFirstChild().getNodeValue());
        			}
        			else if("hour".equals(childNodes.item(j).getNodeName())){  
        				restaurant.setHour(childNodes.item(j).getFirstChild().getNodeValue());
        			}
        			else if("mile".equals(childNodes.item(j).getNodeName())){
        				restaurant.setMile(Float.parseFloat(childNodes.item(j).getFirstChild().getNodeValue()));
        			}
        			else if("menu".equals(childNodes.item(j).getNodeName())){  
        				restaurant.setMenu(childNodes.item(j).getFirstChild().getNodeValue());
        			}/*
        			else if("image".equals(childNodes.item(j).getNodeName())){  
        				restaurant.setImageURL(childNodes.item(j).getFirstChild().getNodeValue());
        			}*/
        			else if("rating".equals(childNodes.item(j).getNodeName())){
        				restaurant.setRate(Float.parseFloat(childNodes.item(j).getFirstChild().getNodeValue()));
        			}
        			else if("latitude".equals(childNodes.item(j).getNodeName())){
        				restaurant.setLat(Float.parseFloat(childNodes.item(j).getFirstChild().getNodeValue()));
        			}
        			else if("longitude".equals(childNodes.item(j).getNodeName())){
        				restaurant.setLng(Float.parseFloat(childNodes.item(j).getFirstChild().getNodeValue()));
        			}
        		}
        	}//end for j 
        	list.add(restaurant);
        }//end for i  
        return list;  
	}
}
